﻿package com.game  
{
	
	import api.dataStruct.InitAPIObj;
	
	import com.game.loading.Loading;
	import com.game.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	
	public class GameLoadThis extends Sprite
	{
		private var _resourceURL:String="resource/"
		private var _loading:Loading;
		private var _apiLoaderInfo:LoaderInfo
		private var _loadingSwf:DisplayObjectContainer;
		private var _logoSwf:DisplayObjectContainer;
		private var _isAPILoadOver:Boolean=false;
		private var _isInitLoadCom:Boolean=false;
		private var _isLogoLoadCom:Boolean=false;
		private var _showLogo:Boolean=false;
		private var _isHttp:Boolean=true;
		private var _loadTitle:String;
		private var _main:*;
		private var _onCom:Function;
		private var _onProg:Function;
		private var _gameW:uint;
		private var _gameH:uint;
		private var _load_loading:LoadSwf;
		private var _load_api:LoadSwf;
		private var _load_logoMovie:LoadSwf;
		private var _verString:String="";
		
		/**
		 * 初始化游戏
		 * @param main:游戏主文件
		 * @param onCom:游戏加载完成
		 * @param gameW:游戏宽度
		 * @param gameH:游戏高度
		 * @param isHttp:是否为http请求
		 * @param isShowLogo:是否显示logo动画
		 **/
		public function GameLoadThis(main:*, onCom:Function,gameW:uint,gameH:uint,isHttp:Boolean=true,isShowLogo:Boolean=true)
		{
			_main = main;
			_onCom = onCom;
			_gameW=gameW;
			_gameH=gameH;
			_showLogo=isShowLogo;
			_isHttp=isHttp;
			if(_isHttp){
				_verString="?v="+Math.random();
			}
			
			
			_load_loading=new LoadSwf();
			_load_api=new LoadSwf();
			_load_logoMovie=new LoadSwf();
		
			
			_main.addChild(this);
			loadGame();
			_load_loading.setLoad(_resourceURL+"loading.swf"+_verString, loadingSwfComplete);
		}
		private function loadGame():void{
			_loadTitle="game:";
			_main.loaderInfo.addEventListener(ProgressEvent.PROGRESS, gameLoadProgress);
			_main.loaderInfo.addEventListener(Event.COMPLETE, gameLoadOver);
			_main.loaderInfo.addEventListener(IOErrorEvent, showLoadError);
		}
		private function showLoadError():void{
			setLoadingClew(_loadTitle+":error!");
		}
		private function gameLoadProgress(event:ProgressEvent) : void
		{
			var _per:uint = int(event.bytesLoaded / event.bytesTotal * 100);
				setLoadingClew(_loadTitle+_per+"%")
		}
		
		private function gameLoadOver(event:Event) : void
		{
			_loadTitle="api:";
			_load_api.setLoad(_resourceURL+"API_3366.swf"+_verString, loadAPIComplete,showLoadError,loadAPIProg);
			
		}
		
		private function loadAPIComplete(loaderInfo:LoaderInfo):void{
			_isAPILoadOver=true;
			_apiLoaderInfo=loaderInfo;
			if(_showLogo){
				loadLogoMovie();
			}else{
				initLoadCom();
			}
		}
		private function setLoadingClew(str:String):void{
			if(_loading!=null){
				_loading.setTxt(str);
			}
		}
		private function loadAPIProg(per:uint):void{
			setLoadingClew(_loadTitle+per+"%");
			
		}
		private function loadLogoMovie():void{
			_loadTitle="logo:";
			_load_logoMovie.setLoad(_resourceURL+"logoMovie.swf"+_verString,loadLogoMovieCom,showLoadError,loadAPIProg);
		}
		private function loadLogoMovieCom(loaderInfo:LoaderInfo):void{
			_isLogoLoadCom=true;
			_logoSwf = loaderInfo.content as DisplayObjectContainer;
			showLogoMovie();
		}
		
		private function showLogoMovie():void{
			_isInitLoadCom=true;
			removeLoadingSwf();
			setCenter(_logoSwf);
			this.addChild(_logoSwf);
			var _mc:MovieClip=_logoSwf["main_mc"] as MovieClip;
			_mc.addFrameScript(_mc.totalFrames-1,initLoadCom);
			_mc.play();
		}
		
		
		public function initLoadCom():void{
			_onCom(_apiLoaderInfo);
			gc();
		}
		
		private function loadingSwfComplete(loaderInfo:LoaderInfo) : void
		{
			_loading = loaderInfo.content as Loading;
			_loadingSwf = loaderInfo.content as DisplayObjectContainer;
			setCenter(_loadingSwf);
			this.addChild(_loadingSwf);
			
		}
		private function removeLoadingSwf():void{
			if (_loadingSwf!=null)
			{
				this.removeChild(_loadingSwf);
			}
		}
		
		private function gc():void{
			
			_main.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, gameLoadProgress);
			_main.loaderInfo.removeEventListener(Event.COMPLETE, gameLoadOver);
			_main.loaderInfo.removeEventListener(IOErrorEvent, showLoadError);
			
			if(_logoSwf!=null){
				this.removeChild(_logoSwf);
			}
			_main.removeChild(this);
			_main=null;
			_loading=null;
			_logoSwf=null;
			_load_api=null;
			_load_loading=null;
			_load_logoMovie=null;
			_loadingSwf=null;
			_apiLoaderInfo=null;
			
			_onCom=null;
			_onProg=null;
		}
		private function setCenter(disObj:DisplayObjectContainer):void{
			disObj.x=(_gameW-disObj.width)/2;
			disObj.y=(_gameH-disObj.height)/2;
		}
		
		
	}
}
